// $Id: LoginDialog.java 16 2011-09-14 17:23:42Z huijzer@gmail.com $
package lime.application.dialog;

import java.util.List;

import lime.Activator;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog that will allow the user to provide login credentials and create a
 * connection with the Documentum repository.
 */
public class LoginDialog extends TitleAreaDialog {

	// Widgets
	private Combo comboRepository;
	private Text textUsername;
	private Text textPassword;

	// Services
	RepositoryService repositoryService;

	/**
	 * Constructor.
	 * 
	 * @param parent
	 *            Parent shell
	 */
	public LoginDialog(Shell parent) {
		super(parent);
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Login");
		setMessage("Please provide the login credentials.");
		return contents;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);

		Composite container = new Composite(area, SWT.NULL);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Read previously entered settings
		IDialogSettings loginSettings = getLoginSettings();
		String repositorySetting = loginSettings.get("repository");
		String usernameSetting = loginSettings.get("username");
		String passwordSetting = loginSettings.get("password");

		new Label(container, SWT.NULL).setText("Repository");
		comboRepository = new Combo(container, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboRepository.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		List<String> repositories = repositoryService.getRepositories();
		for (String repository : repositories) {
			if (repository.equals(repositorySetting)) {
				comboRepository.add(repository, 0);
				comboRepository.select(0);
			} else {
				comboRepository.add(repository);
			}
		}

		new Label(container, SWT.NULL).setText("Username");
		textUsername = new Text(container, SWT.BORDER);
		textUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (StringUtils.isNotBlank(usernameSetting)) {
			textUsername.setText(usernameSetting);
		}

		new Label(container, SWT.NULL).setText("Password");
		textPassword = new Text(container, SWT.PASSWORD | SWT.BORDER);
		textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (StringUtils.isNotBlank(passwordSetting)) {
			textPassword.setText(passwordSetting);
		}

		return area;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(400, 300);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void okPressed() {

		String repository = comboRepository.getText();
		String username = textUsername.getText();
		String password = textPassword.getText();

		try {
			repositoryService.connect(repository, username, password);
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			return;
		}

		IDialogSettings loginSettings = getLoginSettings();
		loginSettings.put("repository", repository);
		loginSettings.put("username", username);
		loginSettings.put("password", password);

		super.okPressed();
	}

	/**
	 * Retrieve the section from dialog settings that contains the settings for
	 * the login dialog.
	 * 
	 * @return Dialog settings for the login dialog.
	 */
	private IDialogSettings getLoginSettings() {
		IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection("login");
		if (settings == null) {
			settings = Activator.getDefault().getDialogSettings().addNewSection("login");
		}
		return settings;
	}

}
